package com.zw.user.service.impl;/*
 *@program:b2c-cloud-store
 *@author:
 *@Time: 2022/11/21  20:18
 *@description: 用户地址接口实现类
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zw.user.mapper.UserAddressMapper;
import com.zw.param.AddressRemoveParam;
import com.zw.pojo.Address;
import com.zw.user.service.UserAddressService;
import com.zw.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    /**
     * 用户查询地址业务
     * 1.  根据用户查询结果
     * 2.  返回封装结果
     * @param userId 参数已校验
     * @return 结果 001 004
     */
    @Override
    public R list(Integer userId) {
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Address> list = userAddressMapper.selectList(queryWrapper);
        R ok = R.ok("查询成功", list);
        log.info("UserAddressServiceImpl.list业务结束，结果:{}",ok);
        return ok;
    }

    /**
     * 用户保存地址业务
     *
     * @param address 参数已校验
     * @return 结果001 004  list<address>
     */
    @Override
    public R save(Address address) {
        int total = userAddressMapper.insert(address);
        if(total == 0){
            log.info("UserAddressServiceImpl.save业务结束，结果:{}","插入地址失败");
            return R.fail("插入地址失败！");
        }
//        复用查询业务
        return list(address.getUserId());
    }

    /**
     * 用户删除地址信息业务
     *
     * @param addressRemoveParam 参数已经校验
     * @return 结果 001 003
     */
    @Override
    public R remove(AddressRemoveParam addressRemoveParam) {
        int rows = userAddressMapper.deleteById(addressRemoveParam.getId());
        if(rows == 0){
            log.info("UserAddressServiceImpl.remove业务结束，结果:{}","删除地址失败！");
            return R.fail("删除地址数据失败");
        }
        return R.ok("删除地址成功！");
    }


}
